package com.drx.controller;

import com.drx.dojo.TabRecord;
import com.drx.service.ReservationService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ReservationController {

    @Autowired
    ReservationService reservationService;

    @RequestMapping(value = "/reservatedSeat",produces = "application/json;charset=utf-8")
    @ResponseBody
    //定义一个方法，用于根据楼层和区域获取到已经被预约的座位
    public List<TabRecord> reservatedSeat(Integer floarNum, String area, Integer beginTime, Integer overTime){
        List<TabRecord> result;
        //调用service层的方法，从数据库中查找到已经被预约的位置
        result=reservationService.reservatedSeat(floarNum,area,beginTime,overTime);
        if (result!=null){
            return result;
        }else{
            return null;
        }
    }

    //定义一个用户开始预约的方法，返回一个map集合，表示结果与错误原因
    @RequestMapping(value = "/beginReservation",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String ,String > beginReservation(Integer addressFloar,String addressArea,Integer addressNum,Integer beginTime,Integer overTime,HttpSession session){
        Map<String ,String > result=new HashMap<String, String>();
        //从session中获取到已登陆用户的学号
        Integer studentNum= (Integer) session.getAttribute("studentNum");
        //判断用户是否登陆
        if (studentNum==null){
            //预约失败
            result.put("reservatResult","false");
            result.put("errorReason","您还未登陆");
        }else{
            //调用service层的方法进行预约座位
            //调用service层的方法判断改用户是否已经有预约了
            TabRecord tabRecord = reservationService.effectiveReservation(studentNum, new Date());
            if (tabRecord!=null){
                //该用户已经有预定了
                result.put("reservatResult","false");
                result.put("errorReason","您已经有预定了");
            }else{
                Integer reservatResult = reservationService.beginReservation(studentNum, addressFloar, addressArea, addressNum, beginTime, overTime);
                //判断是否预约成功
                if (reservatResult!=0){
                    //预约成功
                    result.put("reservatResult","true");
                }else{
                    result.put("reservatResult","false");
                    result.put("errorReason","出现未知错误");
                }
            }

        }
        return result;
    }

    //定义一个方法，用于判断用户是否有已预约的位置
    @RequestMapping(value = "/checkUserReservated",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String ,Object > checkUserReservated(HttpSession session){
        Map<String ,Object> result=new HashMap<String ,Object>();
        //先从session中获取到用户学号
        Integer studentNum= (Integer) session.getAttribute("studentNum");
        if (studentNum==null){
            result.put("result","false");
            result.put("errorReason","您还未登录");
        }else{
            //调用service层的方法，判断用户是否有已预约的座位
            TabRecord tabRecord = reservationService.effectiveReservation(studentNum, new Date());
            result.put("result","true");
            result.put("seat",tabRecord);
        }
        return result;
    }

    //定义一个查询历史预约记录的方法
    @RequestMapping(value = "/historyReservated",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String ,Object > historyReservated(HttpSession session) throws ParseException {
        Map<String ,Object> result=new HashMap<String, Object>();
        //先从session中获取到用户学号
        Integer studentNum= (Integer) session.getAttribute("studentNum");
        if (studentNum==null){
            result.put("result","false");
            result.put("errorReason","您还未登录");
        }else{
            //调用service层的方法，查询历史预约记录
            List<TabRecord> tabRecords = reservationService.selectHistoryByStudentNum(studentNum);
            result.put("result","true");
            result.put("tabRecords",tabRecords);
        }
        return result;
    }

    //定义一个结束预约的方法
    @RequestMapping(value = "/finishReservat",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String ,String > finishReservat(HttpSession session){
        Map<String ,String > result=new HashMap<String, String>();
        //先从session中获取到用户学号
        Integer studentNum= (Integer) session.getAttribute("studentNum");
        if (studentNum==null){
            result.put("result","false");
            result.put("errorReason","您还未登录");
        }else{
            //调用service层的方法，查看是否有预约
            TabRecord tabRecord = reservationService.effectiveReservation(studentNum, new Date());
            if (tabRecord==null){
                result.put("result","false");
                result.put("errorReason","您今日还没有有效的预约哦！");
            }else{
                //调用service层的方法，结束预约
                Integer integer = reservationService.endReservat(studentNum);
                if (integer!=0){
                    result.put("result","true");
                }else{
                    result.put("result","false");
                    result.put("errorReason","出现未知错误");
                }
            }
        }
        return result;
    }
}
